<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/26 0026
 * Time: 9:06
 */
namespace  app\api\model;
use think\Cache;
use think\Db;
use think\Exception;
use think\Request;

class PrizeModel
{
    /**获取奖品信息
     * @param Request $request
     * @return bool|false|\PDOStatement|string|\think\Collection
     */
    public static function getPrize(Request $request){
        //获取身份验证
        $token = $request->param('token',null);
        if(!$token){
            return ['code'=>402,'msg'=>'账号不存在'];
        }
        if(!UserModel::istoken($token)){
            return ['code'=>402,'msg'=>'账号不存在'];
        }
        $page = $request->param('page',1);
        $limit = $request->param('limit',15);
        //查询数据库
        $data = Db::name('prizes')
            ->field('id,p_name,p_picture,p_integral,p_total_awards')
            ->group('p_name')
            ->limit(($page-1)*$limit,$limit)
            ->select();
        return ['code'=>200,'data'=>$data];
    }

    /**领取奖品函数
     * @param Request $request
     * @return array
     */
    public static function getPrizeInfo(Request $request){
        //处理参数
        $token = $request->param('token',null);
        $p_name = $request->param('p_name',null);
        $name = $request->param('name',null);
        $phone = $request->param('phone',null);
        if(!$token||!$p_name||!$name||!$phone){
            return ['code'=>405,'msg'=>'参数有误'];
        }
        //检测token是否存在并拿到user实例
        if(!$user = UserModel::istoken($token)){
            return ['code'=>402,'msg'=>'请先登录'];
        }
        $resul = Db::connect();
        //查询奖品
        $data = $resul->name('prizes')->where('p_name',$p_name)->find();
        //如果今天的奖品一个都没领
        if(!$has = Cache::get(md5($data['p_name']))){
            $time = (strtotime(date('Ymd')) + 86400)-time();
            Cache::set(md5($data['p_name']),($data['p_total_awards']+1),$time);
        }
        //如果今天的奖品领完了
        if($has==1){
            return ['code'=>406,'msg'=>'本奖品今日发放完毕,请选择其他奖品'];
        }
        //检测积分是否足够
        $user['total_integral'] = (int)$user['total_integral']-(int)$data['p_integral'];
        if($user['total_integral']<0){
            return ['code'=>407,'msg'=>'积分不足'];
        }
        $record['openid'] = $user['openid'];
        $record['name'] = $name;
        $record['telephone'] = $phone;
        $record['p_name'] = $data['p_name'];
        $record['p_picture'] = $data['p_picture'];
        $record['time'] = date('Y-m-d H:i:s',time());
        Db::startTrans();
        try{
            //开启事务，删除已经领取的奖品数据，向领奖记录里添加数据，扣除积分
            $data = Db::name('prizes')->where('p_name',$p_name)->find();
            $record['p_account'] = $data['p_account'];
            $record['p_pwd'] = $data['p_pwd'];
            Db::name('prizes')->where('id',$data['id'])->delete();
            $recordId = Db::name('record')->insertGetId($record);
            Db::name('p_management')->where('id',$user['id'])->update($user);
            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            return ['code'=>500,'msg'=>'系统错误'];
        }
        Cache::dec(md5($data['p_name']));
        return ['code'=>200,'data'=>$recordId,'user'=>$user];
    }

    /**获取奖品简略信息
     * @param Request $request
     * @return array
     */
    public static function product_info(Request $request){
        $token = $request->param('token',null);
        $p_name = $request->param('p_name',null);
        if(!$token||!$p_name){
            return ['code'=>402,'msg'=>'参数错误'];
        }
        if(!$user = UserModel::istoken($token)){
            return ['code'=>403,'msg'=>'请登录'];
        }
        $data = Db::name('prizes')->where('p_name',$p_name)->field('id,p_name,p_picture,p_integral,p_total_awards')->find();
        if($user['total_integral']<$data['p_integral']){
            return ['code'=>406,'msg'=>'积分不足'];
        }
        if(!$shu = Cache::get(md5($p_name))){
            $time = (strtotime(date('Ymd')) + 86400)-time();
            $md   = md5($p_name);
            Cache::set($md,($data['p_total_awards']+1),$time);
        }
        if($shu==1){
            return ['code'=>406,'msg'=>'本奖品今日发放完毕,请选择其他奖品'];
        }
        return ['code'=>200,'data'=>$data];
    }

    /**查询获奖记录
     * @param Request $request
     * @return array
     */
    public static function prize_info(Request $request){
        $token = $request->param('token',null);
        $id    = $request->param('id',null);
        if(!$token||!$id){
            return ['code'=>402,'msg'=>'参数错误'];
        }
        if(!$user = UserModel::istoken($token)){
            return ['code'=>403,'msg'=>'请登录'];
        }
        $data = Db::name('record')->where('id',$id)->find();
        return ['code'=>200,'data'=>$data];
    }
}